#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

typedef struct person
{
	char name[20];
	int year;
	int month;
	int day;
	int tag;
}Person;




int main()
{
	int n = 0;
	scanf("%d", &n);
	Person people[100];
	Person tmp;
	int i = 0;
	for (i = 0; i < n; i++)
	{
		scanf("%s %d %d %d", people[i].name, &people[i].year, &people[i].month, &people[i].day);
		people[i].tag = i;
	}
	int j = 0;
	for (i = 0; i < n - 1; i++)
		for (j = 0; j < n - i - 1; j++)
		{
			int temp1 = people[j].year * 10000 + people[j].month * 100 + people[j].day;
			int temp2 = people[j+1].year * 10000 + people[j+1].month * 100 + people[j+1].day;
			if (temp1 > temp2)
			{
				tmp = people[j];
				people[j] = people[j + 1];
				people[j + 1] = tmp;
			}
			else if (temp1 == temp2)
			{
				if (people[j].tag < people[j + 1].tag)
				{
					tmp = people[j];
					people[j] = people[j + 1];
					people[j + 1] = tmp;
				}
			}
		}
	for (i = 0; i < n; i++)
		printf("%s\n", people[i].name);
	return 0;
}